package editro.view {
	import editro.events.system.SystemEvent;
	import editro.events.view.DrawFloateLineEvent;
	import editro.model.AntTaskVOListModel;
	import editro.model.ATECacheModel;
	import editro.model.ATEConfigModel;
	import editro.model.BindingVO;
	import editro.view.components.AntTaskView;
	import editro.view.components.ArchorForLinkage;
	import editro.view.components.ArchorLinkLineOverlay;
	import flash.events.Event;
	import flash.geom.Point;
	import org.robotlegs.mvcs.Mediator;

	/**
	 * ...
	 * @author LEEF LEE_LEAF918@163.COM_QQ969442267
	 */
	public class LinkLineOverlayMediator extends Mediator {
		[Inject]
		public var view:ArchorLinkLineOverlay;
		[Inject]
		public var tasks:AntTaskVOListModel;
		[Inject] 
		public var cacheModel:ATECacheModel;
		[Inject] public var voModel:AntTaskVOListModel;
		public function LinkLineOverlayMediator(){

		}

		override public function onRegister():void {
			super.onRegister();
			view.lineThickness = ATEConfigModel.RELITIVE_LINE_THICKNESS;
			view.lineColor = ATEConfigModel.RELITIVE_LINE_COLOR;
			view.updateLinkArraySignal.add(updateArchorsPointHandler);
			//接收系统全局刷新通知
			eventMap.mapListener(eventDispatcher, SystemEvent.REFRESHOVERLAYER, refreshOverlay);
			eventMap.mapListener(eventDispatcher, DrawFloateLineEvent.START_DRAW_FLOATING_LINE, drwFloatLine);
			eventMap.mapListener(eventDispatcher, DrawFloateLineEvent.UPDATE_STAGE_CURSOR_POSITION, updateStageCursorPoint);
			eventMap.mapListener(eventDispatcher, DrawFloateLineEvent.CANCLE_FLOAT_DRAW, cancleFloatDrawFoo);
		}
		
		private function updateArchorsPointHandler(v:Array):void 
		{
			for (var i:int = 0; i < voModel.bindingList.length; i++) 
			{
				var item:BindingVO = voModel.bindingList[i];
				var tmp1:Point = item._linkStartTartgetVO.viewHolder.getArchorInStagePoint(item._preIndex);
				var tmp2:Point = item._linkEndTargetVO.viewHolder.getArchorInStagePoint(item._rearIndex);
				v.push([tmp1,tmp2]);
			}
		}
		
		private function updateStageCursorPoint(e:DrawFloateLineEvent):void 
		{
			if (cacheModel.isOnFloatDrawingModel) {
				view.refresh();
				//有可能只是单纯的为了刷新，没有传点的值
				view.updateStageCursorPoint(e.pnt);
			}
		}
		
		private function cancleFloatDrawFoo(e:DrawFloateLineEvent):void 
		{
			view.cancleFloat();
			//reset.
			view.refresh();
		}
		
		private function drwFloatLine(e:DrawFloateLineEvent):void 
		{
			view.refresh();
			var stgeP:Point = cacheModel.dragingFloatLineStartedTaskVO.viewHolder.getArchorInStagePoint(cacheModel.dragingFloatLineStartedTaskVOArchorIndex);
			view.begainDrawFloatingLine(stgeP);
		}
		
		private function refreshOverlay(e:Event):void {
			view.refresh();
		}

	}

}